#!/usr/bin/perl -w



use CGI ':standard';



print "Content-type: text/html\n\n";

print "<html>\n";

print "<head>\n";

print "<title>TGrad Material Purchase Price Calculator</title>\n";
print "<style>\n";
print "body {font-family: sans-serif}\n";
print "a:link, a:visited, a:active {font-weight: bold; color: #6699FF}\n";
print "tr.tablecolor2 {background-color: #393939;}\n";
print "tr.tablecolor1 {background-color: #111111;}\n";
print "tr.itemtablecolor2 {background-color: #393939;}\n";
print "tr.itemtablecolor1 {background-color: #111111;}\n";
print "tr.marketgrouptablecolor2 {background-color: #393939;}\n";
print "tr.marketgrouptablecolor1 {background-color: #111111;}\n";
print "table td {\n";
print "    padding: 0.1em 0.5em 0.1em 0.5em;\n";
print "    margin: 0;\n";
print "    margin: 0.1em;\n";
print "    vertical-align: center;\n";    
print "}\n";
print "table.mainstruct td {\n";
print "	border: 0\n";
print "}\n";
print "</style>\n";

print "</head>\n";

print "\n";

print "<body bgcolor=\"#111111\" text=\"#E7E7E7\" link=\"#6699FF\" alink=\"#6699FF\" vlink=\"#6699FF\">\n";

print "\n";

print "<table border=0 cellpadding=0 cellspacing=0 width=100% class=\"mainstruct\">";
print "<tr height=45><td rowspan=2 height=45 width=45><img src=\"http://tgrads.com/images/TheGraduatesLogo.jpg\"></td><td height=45 rowspan=2 align=\"left\"><h1>The Graduates</h1></td>";
print "<td align=\"right\" height=25></td></tr>";
print "<tr height=20><td height=20 align=\"right\"><a href=\"index.html\">Main Menu</a><!-- | <a href=\"salvage.pl\">Salvage Price Calculator</a> | <a href=\"ore.pl\">Ore Price Calculator</a>--></td></tr>";
print "</table><hr>";
print "\n";

print "<font size=\"+3\">TGRAD Ore Price Calculator</font>\n";


print "\n";

print "<p>Enter value in 'Unit' field and press 'Submit' to calculate.<br>Total volume and contract value is displayed at the bottom of the table.<br>Contracts should be made to <a href=\"showinfo:1377//952081939\">Ronan Teisdari</a>.</p>\n";



#@params = param();

#foreach $name ( @params ) {

#   $value = param( $name );

#   print "<p>", $name, "=", $value, "</p>\n";

#}



print "<form method=\"POST\" action=\"http://buy.tgrads.com/ore.pl\">\n";



print "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#C0C0C0\">\n";

print "<tr>\n";

print "<th align=\"left\" width=\"200\">&nbsp;Name&nbsp;</th>\n";

print "<th align=\"right\">&nbsp;Size&nbsp;</th>\n";

print "<th align=\"right\">&nbsp;Price&nbsp;</th>\n";

print "<th align=\"right\">&nbsp;Price/m3&nbsp;</th>\n";

print "<th align=\"right\">&nbsp;Units&nbsp;</th>\n";

print "<th align=\"right\">&nbsp;Volume&nbsp;</th>\n";

print "<th align=\"right\">&nbsp;Value&nbsp;</th>\n";

print "<th>Update</th>\n";

print "</tr>\n";



open( DATA, "./ore.txt") or die "</table><p>Can't open file</body></html>: $!\n";



$totalvolume = "0";

$totalvalue = "0";



while ( $line = <DATA> ) {

   ($name, $price, $size) = split(",", $line);



   $fixed = fixname( $name );

   $count = param( $fixed );



   # remove commas

   $count = join( "", split( ",", $count ) );



   # deal with expodential notation and such

   $count = eval ($count)*1.0;



   $volume = $count*$size;

   $value = $count*$price;



   $totalvolume = $totalvolume + $volume;

   $totalvalue = $totalvalue + $value;



   print "<tr>\n";

   print "<td>","&nbsp;$name","</td>\n";

   print "<td align=\"right\">",formatnum($size),"</td>\n";

   print "<td align=\"right\">",formatnum($price),"</td>\n";

   print "<td align=\"right\">",formatnum($price/$size),"</td>\n";

   print "<td><input type=\"text\" name=\"",$fixed,"\" size=\"10\" value=\"",formatint($count),"\" style=\"text-align: right\"></td>\n";

   print "<td align=\"right\">",formatnum($volume),"</td>\n";

   print "<td align=\"right\">",formatnum($value),"</td>\n";

   print "<td><input type=\"submit\" value=\"Submit\" name=\"B1\"></td>\n";

   print "</tr>\n";

}



print "<tr>\n";

print "<th align=\"right\" colspan=\"5\">Totals&nbsp;</td>\n";

print "<th align=\"right\">",formatnum($totalvolume),"</td>\n";

print "<th align=\"right\">",formatnum($totalvalue),"</td>\n";

print "<th> </th>\n";

print "</tr>\n";



print "</table>\n";



#print "<p><input type=\"reset\" value=\"Reset\" name=\"B2\"></p>\n";

print "</form><br><br><br>\n";



print "</body>\n";

print "\n";

print "</html>\n";



sub fixname ($) {

   my $n = shift;

   return join( "_", split( " ", $n ) );

}



sub formatint($) {

   my $n = shift;

   $n = commify( sprintf( "%.0f", $n ) );

   return $n;

}



sub formatnum($) {

   my $n = shift;

#   my @parts = split( ".", sprintf( "%20.2f", $n ) );

   $n = commify( sprintf( "%.2f", $n ) );

   $n = "&nbsp;$n&nbsp;";

   return $n;

}



# this subroutine adds commas to a number

sub commify ($) {

   my $input = shift;

   $input = reverse( $input );

   $input =~ s<(\d\d\d)(?=\d)(?!\d*\.)><$1,>g;

   return reverse( $input );

}
